using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

// 黑洞技能类，继承自Skill
public class Blackhole_Skill : Skill
{
    // 解锁黑洞技能的按钮
    [SerializeField] private UI_SkillTreeSlot blackHoleUnlockButoon;
    // 黑洞技能是否解锁
    public bool blackholeUnlocked { get; private set; }

    // 黑洞预制体
    [SerializeField] private GameObject blackHolePrefab;

    // 黑洞持续时间
    [SerializeField] private float blackholeDuration;

    // 黑洞最大大小
    [SerializeField] private float maxSize = 15;
    // 黑洞扩大速度
    [SerializeField] private float growSpeed = 1;
    // 黑洞缩小速度
    [SerializeField] private float shrinkSpeed = 2;
    [Space]
    // 攻击次数
    [SerializeField] private int amountOfAttacks = 2;
    // 攻击冷却时间
    [SerializeField] private float cloneAttackCooldown = .3f;

    // 当前黑洞技能控制器
    Blackhole_Skill_Controller currentBlackhole;

    // 重写Start方法
    protected override void Start()
    {
        base.Start();
        // 添加解锁黑洞技能的按钮点击事件
        blackHoleUnlockButoon.GetComponent<Button>().onClick.AddListener(UnlockBlackhole);
    }

    // 重写Update方法
    protected override void Update()
    {
        base.Update();
    }

    // 重写CheckUnlock方法
    protected override void CheckUnlock()
    {
        UnlockBlackhole();
    }

    // 解锁黑洞技能
    private void UnlockBlackhole()
    {
        if (blackHoleUnlockButoon.unlocked)
        {
            blackholeUnlocked = true;
        }
    }

    // 重写CanUseSkill方法，判断是否可以使用技能
    public override bool CanUseSkill()
    {
        return base.CanUseSkill();
    }

    // 重写UseSkill方法，使用黑洞技能
    public override void UseSkill()
    {
        base.UseSkill();
        // 创建黑洞实例
        GameObject newBlackHole = Instantiate(blackHolePrefab, player.transform.position, Quaternion.identity);
        // 获取黑洞技能控制器脚本
        currentBlackhole = newBlackHole.GetComponent<Blackhole_Skill_Controller>();
        // 设置黑洞技能参数并使用
        currentBlackhole.SetupBlackhole(maxSize, growSpeed, shrinkSpeed, amountOfAttacks, cloneAttackCooldown, blackholeDuration);

        AudioManager.instance.PlaySFX(3, player.transform);
        AudioManager.instance.PlaySFX(6, player.transform);
    }

    // 判断黑洞技能是否完成
    public bool SkillCompleted()
    {
        if (!currentBlackhole) return false;

        if (currentBlackhole.playerCanExitState)
        {
            currentBlackhole = null;
            return true;
        }
        return false;
    }

    // 获取黑洞的半径
    public float GetBlackholeRadius()
    {
        return maxSize / 2;
    }
}